<!doctype HTML public "-//W3C//DTD HTML 4.0 Frameset//EN">
<html>
<title>Bloomberg Development Environment</title>
<html>
<pre>
// Copyright 2014-2023 Bloomberg Finance L.P.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// bmqa_messageiterator.h                                             -*-C++-*-
#ifndef INCLUDED_BMQA_MESSAGEITERATOR
#define INCLUDED_BMQA_MESSAGEITERATOR

//@PURPOSE: Provide a mechanism to iterate over the messages of a MessageEvent.
//
//@CLASSES:
//  bmqa::MessageIterator: read-only sequential iterator on &#39;Message&#39; objects
//
//@DESCRIPTION: &#39;bmqa::MessageIterator&#39; is an iterator-like mechanism providing
// read-only sequential access to messages contained into a MessageEvent.
//
///Usage
///-----
// Typical usage of this iterator should follow the following pattern:
//..
//  while (messageIterator.nextMessage()) {
//    const Message&amp; message = messageIterator.message();
//    // Do something with message
//  }
//..


// BMQ
#include &lt;bmqscm_version.h&gt;
#include &lt;bmqa_message.h&gt;

// BDE
#include &lt;ball_log.h&gt;

namespace BloombergLP {

// FORWARD DECLARATION
namespace bmqimp { class Event; }

namespace bmqa {


                         // ==========================
                         // struct MessageIteratorImpl
                         // ==========================

struct MessageIteratorImpl {
    // Struct to hold the impl of the &#39;MessageIterator&#39;; that is so that we can
    // keep the real impl private and use some special cast to manipulate it,
    // without publicly exposing private members.

    // PUBLIC DATA
    bmqimp::Event *d_event_p; // Raw pointer to the event

    bmqa::Message  d_message; // A &#39;Message&#39;, representing a view to the
                              // current message pointing at by this iterator.
                              // This is so that &#39;message&#39; can return a &#39;const
                              // Message&amp;&#39; to clearly indicate the lifetime of
                              // the Message, and so that we only create one
                              // such object per MessageIterator.

    int            d_messageIndex;
                              // Position of &#39;d_message&#39; in the underlying
                              // message event.
};


                           // =====================
                           // class MessageIterator
                           // =====================

class MessageIterator {
    // An iterator providing read-only sequential access to messages contained
    // into a &#39;MesssageEvent&#39;.

  private:
    // CLASS-SCOPE CATEGORY
    BALL_LOG_SET_CLASS_CATEGORY(&quot;BMQA.MESSAGEITERATOR&quot;);

  private:
    // DATA
    MessageIteratorImpl d_impl; // Implementation. Abstracted in its own struct
                                // and private so that we can do some magic to
                                // manipulate it without exposing any
                                // accessors/manipulators (this is wanted since
                                // this class is a public class).

  public:
    // CREATORS
    explicit MessageIterator();
        // Default constructor

    //! MessageIterator(const MessageIterator&amp;) = default;

    //! ~MessageIterator() = default;

    // MANIPULATORS
    //! MessageIterator&amp; operator=(const MessageIterator&amp;) = default;

    bool nextMessage();
        // Advance the position of the iterator to the next message in the
        // event.  Return true if there is a next message and false otherwise.
        // Note that advancing to the next message will invalidate any
        // previously returned bmqa::Message retrieved from the &#39;message()&#39;
        // call.

    // ACCESSORS
    const Message&amp; message() const;
        // Return the message to which the iterator is pointing, if any;
        // otherwise return an invalid message.  Note that &#39;nextMessage&#39; must
        // be called before &#39;message&#39; in order to advance the iterators
        // position to the first message in the event.
};

}  // close package namespace
}  // close enterprise namespace

#endif
</pre>
</body>
</html>
